﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Collections;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Threading;
using mshtml;
using YoutubeExtractor;

namespace FB
{
    public partial class TaiVe : System.Web.UI.Page
    {
        public const string Pattern = @"(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&amp;]v=)|youtu\.be\/)([^""&amp;?\/ ]{11})";
        public string YoutubeID;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.Title = "Tải về từ Youtube ";
                YoutubeID = Request.QueryString["v"];
                if (!string.IsNullOrEmpty(YoutubeID))
                {
                    txtLink.Text = "https://www.youtube.com/watch?v=" + YoutubeID;
                    ResolverYoutube();
                }
            }
        }

        protected void btnGetLink_Click(object sender, EventArgs e)
        {
            YoutubeID = Regex.Match(txtLink.Text, Pattern).Groups[1].Value;
            Response.Redirect("taiyoutube.aspx?v=" + YoutubeID);
        }

        private void ResolverYoutube()
        {
            String GoodUrl;
            bool IsValidLink = DownloadUrlResolver.TryNormalizeYoutubeUrl(txtLink.Text, out GoodUrl);

            if (IsValidLink)
            {
                YoutubeID = Regex.Match(GoodUrl, Pattern).Groups[1].Value;
                IEnumerable<VideoInfo> videoInfos = DownloadUrlResolver.GetDownloadUrls(GoodUrl, false);
                var result = from q in videoInfos
                             where q.AudioBitrate > 0 && q.Resolution > 0
                             select q;

                DisplayResult(result.ToList<VideoInfo>());
            }
        }

        private void DisplayResult(List<VideoInfo> videoList)
        {
            if (videoList != null && videoList.Count > 0)
            {
                pResult.Visible = true;
                imgThumb.ImageUrl = "http://i.ytimg.com/vi/" + YoutubeID + "/mqdefault.jpg";
                hplYoutube.NavigateUrl = "https://www.youtube.com/watch?v=" + YoutubeID;
                hplSource.Text = "https://www.youtube.com/watch?v=" + YoutubeID;
                hplSource.NavigateUrl = "https://www.youtube.com/watch?v=" + YoutubeID;
                StringBuilder sb = new StringBuilder();
                sb.Append("<ul>");
                foreach (VideoInfo item in videoList)
                {
                    this.Title = "Tải về video " + item.Title;
                    lblTitle.Text = item.Title;
                    sb.Append("<li>");
                    sb.Append("<a target='_blank' href='");
                    sb.Append(item.DownloadUrl);
                    sb.Append("'>");
                    sb.Append("<strong>");
                    sb.Append(item.VideoExtension);
                    sb.Append("   ");
                    sb.Append("</strong>");
                    sb.Append(item.Resolution);
                    sb.Append("p");
                    sb.Append("</a>");
                    sb.Append("</li>");
                }
                sb.Append("<ul>");
                ltlResult.Text = sb.ToString();
            }
        }
    }
}